package com.shidaimanong.demo.reflect;

import java.lang.reflect.Field;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class ReflectDemo2 {
    public static void main(String[] args) {
        User user = new User();
        try {
            Field field = user.getClass().getDeclaredField("age");
            field.setAccessible(true);
            field.set(user, 22);
            System.out.println(user.getAge());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class User {
        private Integer age = 18;

        private void setAge(Integer age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }
    }
}

